<template>
	<view>
		<view class="tt">
			热门搜索
		</view>
		<!-- 热门搜索关键词 -->
		<view class="hotKey">
			<view class="kitem" v-for="(item,index) in data.hotKeyList" :key="index" @click="keySearch(item)">
				{{item}}
			</view>
		</view>
		<view class="tt">
			搜素结果
		</view>
		<view class="box">
			<view class="lists-two">
				<view class="item" v-for="(item,index) in data.searchList" :key="index" @click="yu(index)">
					<image :src="item.url" mode="aspectFill" lazy-load></image>
					<view class="title bx">
						<image src="../../static/play.png" mode=""></image>
						<!-- 这里utag返回的文字教程 截取其中一个词显示 -->
						<view>{{item.utag.split(' ')[1]}}</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script setup>
	import {
		onMounted,
		reactive
	} from "vue";

	import {
		onLoad,
		onPullDownRefresh
	} from "@dcloudio/uni-app";

	const data = reactive({
		// 搜索壁纸列表
		searchList: [],
		keyWord: '',
		hotKeyList: [
			'明星', '豪车跑车', '毕业季', '美女', '帅哥', '壁纸', '大海', '影视剧', '可爱动物'
		]
	})

	// 接受路径参数 注意要先引入onLoad
	onLoad((e) => {
		console.log(e.keyWord);
		data.keyWord = e.keyWord
	})


	// 搜索壁纸
	function getSearchList() {
		uni.showLoading({
			title: "壁纸获取中..."
		})
		uni.request({
			method: "GET",
			url: '/api/index.php?c=WallPaper&a=search&start=0&count=99&kw=' + data.keyWord + '&start=0&count=12',
			data: {},
			success: (res) => {
				uni.hideLoading()
				console.log(res);
				data.searchList = res.data.data
				console.log(data.searchList);
				if (data.searchList.length == 0) {
					return uni.showToast({
						title: '暂无此数据',
						icon: "none"
					})
				}
			}
		})
	}

	// 点击热门关键词搜索
	function keySearch(item) {
		uni.showLoading({
			title: "壁纸获取中..."
		})
		uni.request({
			method: "GET",
			url: '/api/index.php?c=WallPaper&a=search&start=0&count=99&kw=' + item + '&start=0&count=20',
			data: {},
			success: (res) => {
				uni.hideLoading()
				console.log(res);
				data.searchList = res.data.data
				console.log(data.searchList);
				if (data.searchList.length == 0) {
					return uni.showToast({
						title: '暂无此数据',
						icon: "none"
					})
				}
			}
		})
	}

	// 预览图片
	function yu(index) {
		// 取出列表中所有的图片链接
		let urls = data.searchList.map(v => v.url)
		uni.previewImage({
			urls: urls, // 预览链接数组
			current: index // 当前图片的缩
		});
	}

	onMounted(() => {
		getSearchList();
	})
</script>

<style scoped lang="scss">
	.content {
		padding: 40rpx 0;
	}

	.tt {
		font-size: 30rpx;
		font-weight: 700;
		margin: 40rpx 20rpx;
	}

	.hotKey {
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		padding: 0 20rpx;
	}

	.kitem {
		// width:115rpx;
		padding: 0 10rpx;
		line-height: 40rpx;
		text-align: center;
		font-size: 26rpx;
		color: #343434;
		background-color: #e7e7e7;
		margin-right: 30rpx;
		margin-bottom: 20rpx;
		border-radius: 5rpx;
	}

	// .hotKey .kitem:nth-child(5n){
	// 	margin-right: 0;
	// }

	.lists-two {
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-wrap: wrap;
		margin-top: 25rpx;
	}

	.lists-two .item {
		position: relative;
		width: 48.5%;
		height: 250rpx;
		margin-bottom: 30rpx;
		overflow: hidden;
	}


	.lists-two .item image {
		width: 100%;
		height: 100%;
		margin-bottom: 10rpx;
		border-radius: 12rpx;
	}

	.lists-two .item .title {
		position: absolute;
		font-size: 28rpx;
		font-weight: 500;
		width: 100%;
		bottom: 0;
		left: 10rpx;
		color: #dedede;
		text-align: center;
	}

	.bx {
		display: flex;
		justify-content: start;
	}

	.lists-two .item .title image {
		width: 32rpx !important;
		height: 32rpx;
		margin-right: 15rpx;
	}



	.box {
		padding: 0 25rpx;
	}

	.cateBox {
		padding: 20rpx 10rpx 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.c-item {
		width: 20%;
		height: 100%;
		text-align: center;
	}

	.c-item image {
		width: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
	}

	.c-item .tit {
		font-size: 28rpx;
		margin-top: 10rpx;
	}

	.title {
		font-size: 34rpx;
		font-weight: 700;
	}


	.h-item {
		height: 140rpx;
		margin-top: 25rpx;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		border-radius: 10rpx;
		background-color: rgba(255, 255, 255, 0.25);
		padding: 10rpx 20rpx;
		box-sizing: border-box;
	}

	.h-item .left image {
		width: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
		margin-right: 30rpx;
	}

	.h-item .info {
		width: 60%;
	}

	.h-item .info .tit {
		font-size: 30rpx;
		font-weight: 700;
	}

	.h-item .info .auther {
		font-size: 26rpx;
		margin-top: 10rpx;
		color: #999;
	}
</style>